﻿using System;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Net;
using System.Threading;
namespace displaycongress_desktop
{
    public class Engine
    {
        private static String ownDir; // Working dir
        private static MainWindow mainWindow;
        public static SessionScreen sessionScreen;
        private static SessionManager sessionManager;
        public static SocketDecoder sDecoder;

        public static Thread packageListenerThread;
        private static AsynchronousSocketListener pkgListener;

        /*
         * Retrieve a encoded file and decode it to the right
         * file format. If successfully decoded: show the file.
         */
        public static void Main() {
            // Start/show the MainWindow
            sDecoder = new SocketDecoder();
            mainWindow = new MainWindow();
            sessionScreen = new SessionScreen();
            sessionManager = new SessionManager();
   
            Application.Run(mainWindow);
            Console.Write("runnen ");
            // Start the AsynchronousSocketListener for listening to possible packages.
        }

        public static void startListening()
        {
            Console.Write("runnen2222 ");
            pkgListener = new AsynchronousSocketListener();
            pkgListener.StartListening();
        }

        public static String getOwnDir()
        {
            return ownDir;
        }

        public static void showMainWindow()
        {
            mainWindow.Show();
            sessionScreen.Hide();

            pkgListener.StopListening();
        }

        public static void showSessionScreen()
        {
            mainWindow.Hide();
            sessionScreen.Show();
            //sessionScreen.LoadFileViewer();
            packageListenerThread = new Thread(new ThreadStart(startListening));
            packageListenerThread.Start();

            sessionScreen.SetIpLabel(sessionManager.showIp());
        }

        public static void ApplicationExit()
        {
            if (packageListenerThread != null)
            {
                packageListenerThread.Abort();
            }
        }
    }
}